<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$id=$_RQ['orderid'];
	$frommid=$_RQ['frommid'];
	if($id){
		$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	if(!$order){
		exi('指定礼包不存在！','error','jy_shoplibao/index/');
	}
	if($order['mid']!=$frommid){
		exi('不是来自所属人的分享！','error','jy_shoplibao/index/');
	}
	$songlog=pdo_get('jy_kabao_songlog',array('status'=>0,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	if($songlog['endtime'] && $songlog['endtime']<time()){
		pdo_update('jy_kabao_songlog',array('status'=>2),array('status'=>0,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		exi('卡片已过期，不能领取！','error','jy_shoplibao/index/');
	}
	$order['statustitle']=$this->orderstatus[$order['status']];;
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	//用户地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['address'] || $order['address']['mobile'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$address['diyfields']=json_decode($address['diyfields'],true);
	$order['addresstext']=model('core/member')->get_address_str($address);
	$order['jiangs']=json_decode($order['jiangs'],true);
	//获取地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['address'] || $order['address']['mobile'])){	
			$address=$order['address'];
	}else{
			$address=$order['addressid'];
	}
	$order['addressinfo']=model('core/member')->get_address_str($address);
	$order['code']=pdo_get('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['codeid']));
	if($order['listid']){
		$order['kabao']=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['listid']),array('id','image','title','desc','price'));
	}else{
		$order['kabao']=json_decode($order['diyinfo'],true);
	}
	if($order['storeid']){
		$order['store']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']),array('id','title'));
	}
	foreach($order['jiangs'] as &$sv){
		$sv['goods']['image']=tomedia($sv['goods']['image']);
	}
	$return['order']=$order;
	$member=model('core/member')->basic($frommid);
	$member['username']=$_SESSION['member']['username'];
	$return['member']=$member;
	$return['post']=array(
		'orderid'=>$order['id'],
		'frommid'=>$frommid,
	);
	set_html('title','优惠券详情');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	if(!$post['sn']){
		exi('请输入卡号！','error');
	}
	$songlog=pdo_get('jy_kabao_songlog',array('sn'=>$post['sn'],'uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
	if(!$songlog['id']){
		exi('该卡号不存在！','error');
	}
	if($songlog['status']==1){
		exi('该卡片已被领取过！','error');
	}
	if($songlog['status']==2){
		exi('该卡片已过期！','error');
	}
	model('jy_kabao/order')->add_songorder($orderid,'',$songlog);
	exi('领取成功！','','jy_shoplibao/orderlist/');
}
?>